
package com.njrsrc.android.app;

import java.util.ArrayList;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.njrsrc.R;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.android.library.vpi.BaseSampleActivity;
import com.njrsrc.android.tasks.ArchivesQueryTask;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.url.RequestUrlProvider;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

/**
 * 档案查询页面
 * 
 * @author Administrator
 */
public class ArchivesActivity extends BaseSampleActivity implements OnClickListener {
    private TitleBar mBar;
    private EditText nameEditText;
    private EditText sexEditText;
    private EditText birthdayEditText;
    private ImageView searchImageView;
//    private String sex;
    private UserInfo userInfo;
    private ProgressDialog progressDialog;
    private List<UserInfo> archivesList;
    public static List<UserInfo> tempList;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            tempList = archivesList;
            switch (msg.what) {
                case MsgID.ARCHIVES_QUERY_FAIL:
                    // 档案查询失败
                    Helpers.closeProgressDialog(progressDialog);
                    Helpers.showToast(ArchivesActivity.this, userInfo.getError_message(),
                            Toast.LENGTH_SHORT);
                    break;
                case MsgID.ARCHIVES_QUERY_SUCCESS:
                    // 档案查询成功
                    Helpers.closeProgressDialog(progressDialog);
                    Intent resultIntent = new Intent();
                    resultIntent.setClass(ArchivesActivity.this, ArchivesListActivity.class);
                    startActivity(resultIntent);
                    break;
                case MsgID.ARCHIVES_QUERY_ERROR:
                    // 档案查询错误
                    Helpers.closeProgressDialog(progressDialog);
                    Helpers.showToast(ArchivesActivity.this, R.string.connectError,
                            Toast.LENGTH_SHORT);
                    Intent errorResultIntent = new Intent();
                    errorResultIntent.setClass(ArchivesActivity.this, ArchivesListActivity.class);
                    startActivity(errorResultIntent);
                    break;

                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_archives);
        initView();
    }

    private void initView() {
        mBar = (TitleBar) findViewById(R.id.activity_title_bar);
        mBar.setTitleText("档案查询");
        mBar.back.setOnClickListener(this);
        nameEditText = (EditText) findViewById(R.id.archives_name_edit);
        sexEditText = (EditText) findViewById(R.id.archives_sex_edit);
        birthdayEditText = (EditText) findViewById(R.id.archives_date_edit);
        searchImageView = (ImageView) findViewById(R.id.archives_search_btn);
        sexEditText.setOnClickListener(this);
        birthdayEditText.setOnClickListener(this);
        searchImageView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tb_ibtn_back:
                finish();
                break;
            case R.id.archives_sex_edit:
//                showSexDialog();
                break;
            case R.id.archives_date_edit:
                new DatePickerDialog(ArchivesActivity.this, new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        birthdayEditText.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                    }
                }, 1990, 0, 1).show();
                break;
            case R.id.archives_search_btn:
                String name = nameEditText.getText().toString();
                // String sex = sexEditText.getText().toString();
                String birthday = birthdayEditText.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    Helpers.showToast(ArchivesActivity.this, R.string.archiveInputName,
                            Toast.LENGTH_SHORT);
                    return;
                }
//                if (TextUtils.isEmpty(sex)) {
//                    Helpers.showToast(ArchivesActivity.this, R.string.archiveInputSex,
//                            Toast.LENGTH_SHORT);
//                    return;
//                }
                if (TextUtils.isEmpty(birthday)) {
                    Helpers.showToast(ArchivesActivity.this, R.string.archiveInputBirthday,
                            Toast.LENGTH_SHORT);
                    return;
                }
                List<String> params = new ArrayList<String>();
//                params.add(AppApplication.mToken);
//                params.add(AppApplication.mUserID);
                params.add(name);
//                params.add(sex);
                // birthday = birthday.replaceAll("-", "");
                double second = Helpers.getTimeMillionsByString(birthday) / 1000;
                params.add(second + "");
                String url = Helpers.combinaStr(RequestUrlProvider.USER_DANG_AN, params);
                userInfo = new UserInfo();
                archivesList = new ArrayList<UserInfo>();
                Object[] args = new Object[4];
                args[0] = mHandler;
                args[1] = url;
                args[2] = userInfo;
                args[3] = archivesList;
                progressDialog = Helpers
                        .showProgressDialog(ArchivesActivity.this, R.string.loading);
                new ArchivesQueryTask().execute(args);
                break;

            default:
                break;
        }
    }

//    public void showSexDialog() {
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        builder.setTitle("性别");
//        sex = "男";
//        builder.setSingleChoiceItems(R.array.sex, 0,
//                new android.content.DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int which) {
//                        sex = getResources().getStringArray(R.array.sex)[which];
//                    }
//                });
//        builder.setPositiveButton("确定 ", new DialogInterface.OnClickListener() {
//            public void onClick(DialogInterface dialog, int which) {
//                sexEditText.setText(sex);
//            }
//        });
//        builder.create().show();
//    }
}
